package cn.edu.scujcc;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DynamicService {
	@Autowired
	private DynamicRepositroy repo;
	
	public List<Dynamic> getAllDynamic(){
		List<Dynamic>result = null;
		result = repo.findAll();
		return result;
	}
	public Dynamic getDynamic(String id) {
		Dynamic result = null;
		Optional<Dynamic> op = repo.findById(id);
		if (op.isPresent()) {
			result = op.get();
			}
		return result;
		}
	
	public Dynamic createComment(String id) {
		Dynamic result = null;
		Optional<Dynamic> op = repo.findById(id);
		if (op.isPresent()) {
			result = op.get();
			}
		return result;
		}
	public Dynamic createDynamic(Dynamic d) {
		Dynamic result = null;
		result = repo.save(d);
		return result;
	}
	public Dynamic updateDynamic(Dynamic d) {
		Dynamic result = null;
		result = repo.findById(d.getId()).get();
		if(result !=null) {
		if(d.getName()!=null){
			result.setName(d.getName());
		}
		if(d.getThumbsup()!=null){
			result.setThumbsup(d.getThumbsup());
		}
		if(d.getComment()!=null){
			result.setComment(d.getComment());
		}
		if(d.getMusic()!=null){
			result.setMusic(d.getMusic());
		}
		result = repo.save(result);
		}
		return result;
	}
}